//
//  TimerActionsheetView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/4.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "TimerActionsheetView.h"
#import "ReportCell.h"
@interface TimerActionsheetView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation TimerActionsheetView

-(instancetype)initWithFrame:(CGRect)frame WithTitlesArray:(NSArray *)titles{
     self.titlesArray = titles;
    if (self = [super initWithFrame:frame]) {
        [self initTabliewAndSetSubviews];
    }
    return self;
}

-(void)initTabliewAndSetSubviews{
//    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    topView.backgroundColor = [UIColor blackColor];
//    topView.alpha = 0.5f;
//    [self addSubview:topView];
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableView];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.bottom.right.equalTo(self);
//        make.height.mas_equalTo(HEIGHT/2);
//    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ReportCell class] forCellReuseIdentifier:@"ReportCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportCell"];
    cell.namelabel.text = self.titlesArray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (self.timeBlock) {
      self.timeBlock(cell.namelabel.text);
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (HEIGHT/2 - scaleTopAndBottom(50.f))/6;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(view);
        make.height.mas_equalTo(1.0f);
    }];
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeTheChoose) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(view);
        make.top.equalTo(lineView.mas_bottom);
    }];
    return view;
}
#pragma mark - UIScrollViewDelegate和取消区头的停滞效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]){
    }
    CGFloat sectionHeaderHeight = scaleTopAndBottom(50);
    if (scrollView.contentOffset.y <=sectionHeaderHeight && scrollView.contentOffset.y >=  0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
/**
 关闭菜单
 */
-(void)closeTheChoose{
    if (self.closeBlock) {
        self.closeBlock();
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return scaleTopAndBottom(50.f);
}
@end
